package com.qf.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitPublishListener  implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    private RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;

        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String s) {
//        ack 如果为true  代表消息发送到了服务器 否则没到
        if(ack){
            System.err.println("消息送达了服务器");
        }else {
            System.err.println("消息meiyou送达");
        }
    }

    @Override
    public void returnedMessage(Message message, int i, String s, String s1, String s2) {
//        如果当前方法执行了 意味着消息没有发送到指定的队列
        System.out.println("消息没有发送到指定的队列");
    }
}
